import { type GreenManufacturingParams } from '../apis';
import { SubjectType, getTagYear } from '@/apis';
import { type FilterMenuProps, ComponentType, type IResult } from 'vc/src/web';
import { getAreaConfig, getIndustryConfig, getTagTypeConfig } from '@/utils/filterConfig';
import { getGreenCertificationDepartment } from '../apis';
import { transferYearToMultipleSelector } from '@/utils';

export function useScreen() {
  const queryData = reactive<GreenManufacturingParams>({
    pageNum: 1,
    pageSize: 15,
    hbSubjectType: SubjectType.GREENSUPPLYCHAIN,
    sortRule: 'noticeDate:desc',
    searchTxt: '',
    provinceCode: '',
    cityCode: '',
    countryCode: '',
    industryCategoryCodeLv1: '',
    industryCategoryCodeLv2: '',
    industryCategoryCodeLv3: '',
    departmentCode: '',
    tagYear: '',
    tagTypeCode: '',
    noticeStartDate: '',
    noticeEndDate: ''
  });

  const menus = ref<Array<FilterMenuProps>>([
    {
      ...getAreaConfig().value,
      submitFn: (val: IResult) => {
        const { lv1, lv2, lv3 } = val;
        queryData.provinceCode = lv1.join();
        queryData.cityCode = lv2.join();
        queryData.countryCode = lv3.join();
      }
    },
    {
      ...getIndustryConfig().value,
      submitFn: (val: IResult) => {
        const { lv1, lv2, lv3 } = val;
        queryData.industryCategoryCodeLv1 = lv1.join();
        queryData.industryCategoryCodeLv2 = lv2.join();
        queryData.industryCategoryCodeLv3 = lv3.join();
      }
    },
    {
      ...getTagTypeConfig(SubjectType.GREENSUPPLYCHAIN).value,
      submitFn: (val: string) => {
        queryData.tagTypeCode = val;
      }
    },
    {
      ...getPostLevelConfig().value,
      submitFn: (val: string) => {
        queryData.departmentCode = val;
      }
    },
    {
      ...getTagYearConfig().value,
      submitFn: (val: string) => {
        queryData.tagYear = val;
      }
    },
    {
      title: '披露日期',
      component: ComponentType.DATE,
      type: 'daterange',
      submitFn: (val: Array<string>) => {
        queryData.noticeStartDate = val[0];
        queryData.noticeEndDate = val[1];
      }
    }
  ]);

  return {
    queryData,
    menus
  };
}

function getPostLevelConfig() {
  const { data } = useRequest(getGreenCertificationDepartment('4'));

  return computed(() => {
    return {
      title: '认定级别',
      component: ComponentType.MULTIPLE,
      options: data,
      submitFn: () => {}
    };
  });
}

function getTagYearConfig() {
  const { data } = useRequest(getTagYear(SubjectType.GREENSUPPLYCHAIN));
  const options = computed(() => {
    return transferYearToMultipleSelector(data.value || []);
  });

  return computed(() => {
    return {
      title: '认定年度',
      component: ComponentType.MULTIPLE,
      options,
      customYear: true,
      submitFn: () => {}
    };
  });
}
